package com.sino.androiddesign.database

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import com.sino.androiddesign.R
import com.sino.androiddesign.database.greenDataBase.GreenUtils
import com.sino.androiddesign.database.greenDataBase.User
import com.sino.androiddesign.database.greendao.UserDao
import com.sino.androiddesign.database.nativeDataBase.SQLiteUtils
import com.sino.androiddesign.database.nativeDataBase.Student
import com.sino.androiddesign.database.objectBoxDataBase.ObjectBoxHelper
import com.sino.androiddesign.database.objectBoxDataBase.Person
import com.sino.androiddesign.database.objectBoxDataBase.Person_
import io.objectbox.Property
import kotlinx.android.synthetic.main.activity_data_base.*

/**
 * 数据库
 * 三种:
 * 1: 原生数据库
 * 2: greenDao
 * 3: ObjectBox  : https://docs.objectbox.io/getting-started
 */
class DataBaseActivity : AppCompatActivity() {
    val TAG = "DataBaseActivity"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_data_base)

        initNativeDataBase()
        initGreenDaoDataBase()
        initObjectBoxDataBase()
    }


    /**
     * 原生数据库的相关操作
     */
    private fun initNativeDataBase() {

        data_native_add.setOnClickListener {
            val nameText = data_name_et.text.toString()
            val clsText = data_class_et.text.toString()

            SQLiteUtils.addStudent(Student(nameText, clsText))
        }

        data_native_update.setOnClickListener {
            val nameText = data_name_et.text.toString()
            val clsText = data_class_et.text.toString()
            SQLiteUtils.updateStudent(clsText, nameText)
        }

        data_native_query.setOnClickListener {
            val nameText = data_name_et.text.toString()
            SQLiteUtils.queryStudent(nameText)
        }

        data_native_delete.setOnClickListener {
            val nameText = data_name_et.text.toString()
            SQLiteUtils.deleteStudent(nameText)
        }

    }

    /**
     * GreenDao数据库的相关操作
     */
    private fun initGreenDaoDataBase() {

        data_gd_add.setOnClickListener {
            val nameText = data_name_et.text.toString()
            val clsText = data_class_et.text.toString()

            val insert = GreenUtils.userDao!!.insert(User(null, nameText, clsText))
            Log.e(TAG, "green insert => $insert")
        }

        data_gd_update.setOnClickListener {
            val nameText = data_name_et.text.toString()
            val clsText = data_class_et.text.toString()
            val unique = GreenUtils.userDao!!.queryBuilder().where(UserDao.Properties.Name.eq(nameText)).unique()
            GreenUtils.userDao.update(User(unique.id, nameText, clsText))
        }

        data_gd_query.setOnClickListener {
            val nameText = data_name_et.text.toString()
            val list = GreenUtils.userDao!!.queryBuilder().where(UserDao.Properties.Name.eq(nameText)).list()
            Log.e(TAG, "green insert => ${list.toString()}")
        }

        data_gd_delete.setOnClickListener {
            val nameText = data_name_et.text.toString()
            val unique = GreenUtils.userDao!!.queryBuilder().where(UserDao.Properties.Name.eq(nameText)).unique()
            GreenUtils.userDao.delete(unique)
        }

    }


    /**
     * ObjectBox 数据库相关操作
     */
    private fun initObjectBoxDataBase() {

        val boxFor = ObjectBoxHelper.get()!!.boxFor(Person::class.java)

        data_ob_add.setOnClickListener {
            val nameText = data_name_et.text.toString()
            val clsText = data_class_et.text.toString()
            val put = boxFor.put(Person(name = nameText, desk = clsText))
            Log.e(TAG,"存储 => $put")
        }
        data_ob_update.setOnClickListener {
            val nameText = data_name_et.text.toString()
            val clsText = data_class_et.text.toString()
            val findFirst = boxFor.query()
                    .equal(Person_.name, nameText)
                    .build()
                    .findFirst()!!
            findFirst.desk = clsText
            val put = boxFor.put(findFirst)
            Log.e(TAG,"更新  => $put")

        }
        data_ob_query.setOnClickListener {
            val nameText = data_name_et.text.toString()
            val clsText = data_class_et.text.toString()
            val findFirst = boxFor.query()
                    .equal(Person_.name, nameText)
                    .build()
                    .findFirst()!!
            Log.e(TAG,"查询  => ${findFirst.name}+ ${findFirst.desk}")
        }
        data_ob_delete.setOnClickListener {
            val nameText = data_name_et.text.toString()
            val findFirst = boxFor.query()
                    .equal(Person_.name, nameText)
                    .build()
                    .findFirst()

            if (findFirst != null) {
                boxFor.remove(findFirst.id)
            }

        }

    }
}
